#coding:utf8
"""
获取实时数据，每天收盘后,因此每天爬取的时间的有效区间为16：00-23：30
如果该时间段未能获取数据，则需从历史数据中获取
"""
import os,re,json
import requests
import pandas as pd
import efinance as ef
from datetime import date,datetime


def main(date):
    #获取当前时间
    now = datetime.now()
    cas = now.strftime("%H:%M:%S")
    t1 = datetime.strptime("00:10:00","%H:%M:%S").strftime("%H:%M:%S")
    t2 = datetime.strptime("23:58:00", "%H:%M:%S").strftime("%H:%M:%S")
    # if cas < t1:
    #     print(f"当前时间: {cas}, 暂时无法获取当天收盘数据")
    #     return None
    # elif cas > t2:
    #     print(f"当前时间: {cas}, 无法及时获取当天数据，请从历史数据中获取当天数据")
    #     return None

    if not date:
        now = datetime.now()
        td = now.strftime("%Y-%m-%d")
    else:
        td = date
    #td = "2022-11-29"

    df1 = ef.stock.get_realtime_quotes()
    codes_list = df1['股票代码'].tolist()
    cnt = 0
    data = []
    for code in codes_list:
        cnt += 1
        print(f"processing: {code}, {cnt}/{len(codes_list)}")
        try:
            df2 = ef.stock.get_history_bill(code)
            df22 = df2[-1:len(df2)]
            df3 = df1[(df1['股票代码'] == code) & (df1['最新交易日'] == td)]
            df3 = df3.copy()
            df3.rename(columns={"最新交易日":"日期"}, inplace=True)
            df4 = pd.merge(df22, df3, how='outer', on=['股票代码','股票名称', '涨跌幅', '日期'])
            tmp = df4.values
            data.append(df4)
        except Exception as e:
            print(f"{e.__str__()}")

    df = pd.concat(data, ignore_index=True)

    #today = td.today()
    #d1 = today.strftime("%Y-%m-%d")
    fileName = td + '.csv'
    filePath = os.path.join("../data/realtime", fileName)
    df.to_csv(filePath, sep=',', encoding='utf8')


if __name__=="__main__":
    #date = '2022-12-09'
    date = None
    main(date)
